package cn.cloudreal.cbms.resmonitor.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.ServletActionContext;

import cn.cloudreal.cbms.common.util.Constants;
import cn.cloudreal.cbms.common.util.StringUtils;
import cn.cloudreal.cbms.mediaPrice.service.MediaPriceService;
import cn.cloudreal.cbms.resmonitor.service.ResMonitorService;
import cn.cloudreal.cbms.vo.MediaPrice;
import cn.cloudreal.cbms.vo.Mediasumm;
import cn.cloudreal.cbms.vo.PutInPlanMonth;
import cn.cloudreal.cbms.vo.SourcePlanYear;
import cn.cloudreal.cbms.vo.UserInfo;

import com.opensymphony.xwork2.ActionSupport;

public class UploadSourcePlanAction extends ActionSupport {

    /** 资源监控服务 */
    private ResMonitorService resMonitorService = null;
    private MediaPriceService mediaPriceService = null; // 媒体价格service
    /** 媒体list */
    private List<Mediasumm> mediaSummaries = null;
    /** 地市 */
    private String cityKey;
    private String cityValue;
    private String costType = "";// 费用类型
    private String costSubtype = "";// 费用子类
    /** 年份 */
    private int year;

    private File uploadFile_01 = null;// 预算(年)文件
    private String uploadFile_01FileName;
    private String uploadFile_01ContentType;
    private String uploadErrorMessage = "";// 错误信息
    private String noError = "";

    private File uploadFile_02 = null;// 预算(月)文件
    private String uploadFile_02FileName;
    private String uploadFile_02ContentType;
    private String uploadErrorMessage_01 = "";// 错误信息
    private String noError_01 = "";

    /**
     * 跳转到预算上传页面
     * 
     * @return
     */
    public String toUploadSourcePlan() {
        // 初始化相关参数和下拉框
        Calendar ca = Calendar.getInstance();
        year = ca.get(Calendar.YEAR);
        // 获取当前session对象中所属城市
        UserInfo userInfo = (UserInfo) ServletActionContext.getRequest().getSession().getAttribute(Constants.SESSIONPRKEY);
        cityKey = userInfo.getUsercity();
        cityValue = userInfo.getCityName();
        // 根据合同年份和媒体名称查找当前所有的媒体list
        mediaSummaries = resMonitorService.getMediaSummariesByYear(year, null, cityKey);
        return SUCCESS;
    }

    /**
     * 预算年导入
     * 
     * @return
     */
    public String doUploadSourcePlanByYear() {
        String fliePath = "";// 上传文件路径
        String realpath = ServletActionContext.getServletContext().getRealPath("/upload"); // 获得web应用的真实路径
        String fileName = "years_" + StringUtils.getMathAndTime() + ".xlsx";
        File savefile = new File(realpath, fileName); // 参数1：保存路径，参数2：文件名
        try {
            FileUtils.copyFile(uploadFile_01, savefile);
        }
        catch (IOException e) {
            e.printStackTrace();
            return ERROR;
        }
        fliePath = realpath + "/" + fileName;

        // 解析excel
        Workbook workbook = null;
        try {
            workbook = this.create(new FileInputStream(fliePath));
        }
        catch (Exception e) {
            e.printStackTrace();
            this.deleteFile(fliePath);
            return ERROR;
        }

        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            String nowDate = sf.format(date);
            UserInfo userInfo = (UserInfo) ServletActionContext.getRequest().getSession().getAttribute(Constants.SESSIONPRKEY);
            String city = userInfo.getUsercity();// 归属地市
            for (int k = 0; k < workbook.getNumberOfSheets(); k++) {
                Sheet sheet = workbook.getSheetAt(k);
                String sheetName = sheet.getSheetName(); // sheet名字
                int rows = sheet.getPhysicalNumberOfRows();
                for (int r = 2; r < rows; r++) {
                    Row row = (Row) sheet.getRow(r);
                    if (row == null) {
                        break;
                    }
                    if (row != null) {
                        boolean bool = true;// 提示
//                        System.out.println(this.getValue(row.getCell(0)).trim());
                        String mediaName = this.getValue(row.getCell(0)).trim();// 媒体名称
                        MediaPrice mediaPrice = null;// 媒体价格
                        if(!"".equals(mediaName) && StringUtils.getStringLength(mediaName)>160){
                            uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，媒体名称长度不能大于80汉字(160字符)！<br/>";
                            bool = false;
                        }else if(!"".equals(mediaName) && StringUtils.StringFilter_Bool(mediaName)){
                            uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，媒体名称含有特殊字符！<br/>";
                            bool = false;
                            continue;
                        }else{
                            List<MediaPrice> mpList = mediaPriceService.findMediaPriceByMediaIDOrName_01(mediaName, city, nowDate);// 根据媒体名称查询
                            if (mpList != null && mpList.size() != 0) {
                                mediaPrice = mpList.get(0);
                            }else{
                                uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，媒体名称不能为空、媒体名称不存在或媒体名称不可用！<br/>";
                                bool = false;
                                continue;
                            }
                        }
                        
                        SourcePlanYear sourcePlanYear_Bool = null;
                        if (mediaPrice != null) {
                            sourcePlanYear_Bool = resMonitorService.getSourceIsExist(String.valueOf(mediaPrice.getPriceID()), costType, costSubtype);
                        }

                        if (sourcePlanYear_Bool != null) {// 存在
                            uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，预算(年)数据已存在！<br/>";
                            bool = false;
                            continue;
                        }

                        if (mediaPrice == null) {
                            continue;
                        }else{
                            SourcePlanYear sourcePlanYear = new SourcePlanYear();
                            sourcePlanYear.setId_price(mediaPrice.getPriceID());// 媒体价格ID
                            sourcePlanYear.setMediaID(String.valueOf(mediaPrice.getMediaID()));// 媒体ID
                            sourcePlanYear.setMediaName(mediaName);// 媒体名称
                            sourcePlanYear.setCostType(costType);// 费用类型
                            sourcePlanYear.setCostSubtype(costSubtype);// 费用子类
                            sourcePlanYear.setCity(cityKey);// 城市
                            sourcePlanYear.setPlanYear(String.valueOf(year));// 年

                            String fixSourceAmount = this.getValue(row.getCell(1)).trim();// 固定资源
                            if (!"".equals(fixSourceAmount) && isFloathString(fixSourceAmount) && fixSourceAmount.length() <12) {
                                sourcePlanYear.setFixSourceAmount(Double.parseDouble(fixSourceAmount));
                            }
                            else {
                                uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，固定资源不能为空或不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }

                            String floatSourceAmount = this.getValue(row.getCell(2)).trim();// 浮动资源
                            if (!"".equals(floatSourceAmount) && isFloathString(floatSourceAmount) && floatSourceAmount.length() <12) {
                                sourcePlanYear.setFloatSourceAmount(Double.parseDouble(floatSourceAmount));
                            }
                            else {
                                uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，浮动资源不能为空或不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }

                            String unPromotionAmount = this.getValue(row.getCell(3)).trim();// 购买资源
                            if (!"".equals(unPromotionAmount) && isFloathString(unPromotionAmount) && unPromotionAmount.length() <12) {
                                sourcePlanYear.setUnPromotionAmount(Double.parseDouble(unPromotionAmount));
                            }
                            else {
                                uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，购买资源不能为空或不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }

                            String promotionAmount = this.getValue(row.getCell(4)).trim();// 常规赠送金额
                            if (!"".equals(promotionAmount) && isFloathString(promotionAmount) && promotionAmount.length() <12) {
                                sourcePlanYear.setPromotionAmount(Double.parseDouble(promotionAmount));
                            }
                            else {
                                uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，常规赠送金额不能为空或不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }
                            String promotionUsedRatio = this.getValue(row.getCell(5)).trim();// 常规资源折扣率
                            if (!"".equals(promotionUsedRatio) && isFloathString(promotionUsedRatio)) {
                                if (Double.parseDouble(promotionUsedRatio) <= 100 && Double.parseDouble(promotionUsedRatio) >= 0) {
                                    sourcePlanYear.setPromotionUsedRatio(Double.parseDouble(promotionUsedRatio));
                                }
                                else {
                                    uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，常规资源折扣率应是0-100的数！<br/>";
                                    bool = false;
                                }
                            }
                            else {
                                uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，常规资源折扣率不能为空或不是数字！<br/>";
                                bool = false;
                            }
                            if (!"".equals(this.getValue(row.getCell(6)).trim()) && isValidDate(this.getValue(row.getCell(6)).trim())) {
                                sourcePlanYear.setPromotionAmountTime(this.getValue(row.getCell(6)).trim());// 常规资源有效期
                            }
                            else {
                                uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，常规资源有效期不能为空或格式错误！<br/>";
                                bool = false;
                            }

                            String increPromotionAmount = this.getValue(row.getCell(7)).trim();// 增量赠送金额
                            if (!"".equals(increPromotionAmount) && isFloathString(increPromotionAmount) && increPromotionAmount.length() <12) {
                                sourcePlanYear.setIncrePromotionAmount(Double.parseDouble(increPromotionAmount));
                            }
                            else {
                                uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，增量赠送金额不能为空或不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }
                            String increPromotionUsedRatio = this.getValue(row.getCell(8)).trim();// 增量资源折扣率
                            if (!"".equals(increPromotionUsedRatio) && isFloathString(increPromotionUsedRatio)) {
                                if (Double.parseDouble(increPromotionUsedRatio) <= 100 && Double.parseDouble(increPromotionUsedRatio) >= 0) {
                                    sourcePlanYear.setIncrePromotionUsedRatio(Double.parseDouble(increPromotionUsedRatio));
                                }
                                else {
                                    uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，增量资源折扣率应是0-100的数！<br/>";
                                    bool = false;
                                }
                            }
                            else {
                                uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，增量资源折扣率不能为空或不是数字！<br/>";
                                bool = false;
                            }
                            System.out.println(isValidDate(this.getValue(row.getCell(9)).trim()));
                            if (!"".equals(this.getValue(row.getCell(9)).trim()) && isValidDate(this.getValue(row.getCell(9)).trim())) {
                                sourcePlanYear.setIncrePromotionAmountTime(this.getValue(row.getCell(9)).trim());// 增量资源有效期
                            }
                            else {
                                uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，增量资源有效期不能为空或格式错误！<br/>";
                                bool = false;
                            }

                            String returnSourceAmount = this.getValue(row.getCell(10)).trim();// 年底一次性返还金额
                            if (!"".equals(returnSourceAmount) && isFloathString(returnSourceAmount) && returnSourceAmount.length() <12) {
                                sourcePlanYear.setReturnSourceAmount(Double.parseDouble(returnSourceAmount));
                            }
                            else {
                                uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，年底一次性返还金额不能为空或不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }
                            String returnSourceUsedRatio = this.getValue(row.getCell(11)).trim();// 返还资源折扣率
                            if (!"".equals(returnSourceUsedRatio) && isFloathString(returnSourceUsedRatio)) {
                                if (Double.parseDouble(returnSourceUsedRatio) <= 100 && Double.parseDouble(returnSourceUsedRatio) >= 0) {
                                    sourcePlanYear.setReturnSourceUsedRatio(Double.parseDouble(returnSourceUsedRatio));
                                }
                                else {
                                    uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，返还资源折扣率应是0-100的数！<br/>";
                                    bool = false;
                                }
                            }
                            else {
                                uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，返还资源折扣率不能为空或不是数字！<br/>";
                                bool = false;
                            }
                            if (!"".equals(this.getValue(row.getCell(12)).trim()) && isValidDate(this.getValue(row.getCell(12)).trim())) {
                                sourcePlanYear.setReturnSourceAmountTime(this.getValue(row.getCell(12)).trim());// 返还资源有效期
                            }
                            else {
                                uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，返还资源有效期不能为空或格式错误！<br/>";
                                bool = false;
                            }

                            double sum = 0;// 投放量总和
                            if (sourcePlanYear.getFixSourceAmount() != null && sourcePlanYear.getFloatSourceAmount() != null) {
                                sum = sourcePlanYear.getFixSourceAmount() + sourcePlanYear.getFloatSourceAmount();
                                sourcePlanYear.setPlanTotalAmount(sum);// 投放量总和
                            }

                            double num = 0;
                            if (sourcePlanYear.getUnPromotionAmount()!= null && sourcePlanYear.getPromotionAmount() != null && sourcePlanYear.getIncrePromotionAmount() != null
                                    && sourcePlanYear.getReturnSourceAmount() != null) {
                                 num = sourcePlanYear.getUnPromotionAmount()+sourcePlanYear.getPromotionAmount() + sourcePlanYear.getIncrePromotionAmount()
                                        + sourcePlanYear.getReturnSourceAmount();//购买+常规+增量+返还
                                // System.out.println(sourcePlanYear.getPromotionAmount());
                                // System.out.println(sourcePlanYear.getIncrePromotionAmount());
                                // System.out.println(sourcePlanYear.getReturnSourceAmount());
                                // System.out.println(num + "___" +
                                // sourcePlanYear.getUnPromotionAmount());
                                if (num != sum) {
                                    uploadErrorMessage += sheetName + ",第" + (r + 1) + "行，固定资源+浮动资源总和不等于购买资源+常规赠送金额+增量赠送金额+年底一次性返还金额总和！<br/>";
                                    bool = false;
                                }
                            }

                            if (bool) {
                                resMonitorService.saveSourcePlanYear(sourcePlanYear);// 添加预算(年)
                                this.deleteFile(fliePath);// 删除文件
                            }
                        }
                    }
                }
            }
            if ("".equals(uploadErrorMessage)) {
                noError = "noError";
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            this.deleteFile(fliePath);// 删除文件
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 预算月导入
     * 
     * @return
     */
    public String doUploadSourcePlanByMonth() {
        String fliePath = "";// 上传文件路径
        String realpath = ServletActionContext.getServletContext().getRealPath("/upload"); // 获得web应用的真实路径
        String fileName = "month_" + StringUtils.getMathAndTime() + ".xlsx";
        File savefile = new File(realpath, fileName); // 参数1：保存路径，参数2：文件名
        try {
            FileUtils.copyFile(uploadFile_02, savefile);
        }
        catch (IOException e) {
            e.printStackTrace();
            return ERROR;
        }
        fliePath = realpath + "/" + fileName;

        // 解析excel
        Workbook workbook = null;
        try {
            workbook = this.create(new FileInputStream(fliePath));
        }
        catch (Exception e) {
            e.printStackTrace();
            this.deleteFile(fliePath);
            return ERROR;
        }

        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            String nowDate = sf.format(date);
            UserInfo userInfo = (UserInfo) ServletActionContext.getRequest().getSession().getAttribute(Constants.SESSIONPRKEY);
            String city = userInfo.getUsercity();// 归属地市
            for (int k = 0; k < workbook.getNumberOfSheets(); k++) {
                Sheet sheet = workbook.getSheetAt(k);
                String sheetName = sheet.getSheetName(); // sheet名字
                int rows = sheet.getPhysicalNumberOfRows();
                for (int r = 2; r < rows; r++) {
                    Row row = (Row) sheet.getRow(r);
                    if (row == null) {
                        break;
                    }
                    if (row != null) {
                        boolean bool = true;// 提示
                        String mediaName = this.getValue(row.getCell(0)).trim();// 媒体名称
                        MediaPrice mediaPrice = null;// 媒体价格
                        
                        if(!"".equals(mediaName) && StringUtils.getStringLength(mediaName)>160){
                            uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，媒体名称长度不能大于80汉字(160字符)！<br/>";
                            bool = false;
                        }else if(!"".equals(mediaName) && StringUtils.StringFilter_Bool(mediaName)){
                            uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，媒体名称含有特殊字符！<br/>";
                            bool = false;
                            continue;
                        }else{
                            List<MediaPrice> mpList = mediaPriceService.findMediaPriceByMediaIDOrName_01(mediaName, city, nowDate);// 根据媒体名称查询
                            if (mpList != null && mpList.size() != 0) {
                                mediaPrice = mpList.get(0);
                            }else{
                                uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，媒体名称不能为空、媒体名称不存在或媒体名称不可用！<br/>";
                                bool = false;
                                continue;
                            }
                        }

                        SourcePlanYear sourcePlanYear_Bool = null;
                        if (mediaPrice != null) {
                            sourcePlanYear_Bool = resMonitorService.getSourceIsExist(String.valueOf(mediaPrice.getPriceID()), costType, costSubtype);
                        }

                        if (sourcePlanYear_Bool == null) {// 不存在
                            bool = false;
                            uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，预算年不存在！<br/>";
                            continue;
                        }

                        PutInPlanMonth putInPlanMonth = new PutInPlanMonth();
                        putInPlanMonth.setSourceId(sourcePlanYear_Bool.getId());// 年度ID
                        putInPlanMonth.setId_price(mediaPrice.getPriceID());// 媒体价格ID
                        putInPlanMonth.setMediaID(String.valueOf(mediaPrice.getMediaID()));// 媒体ID
                        putInPlanMonth.setMediaName(mediaName);// 媒体名称
                        putInPlanMonth.setCostType(costType);// 费用类型
                        putInPlanMonth.setCostSubtype(costSubtype);// 费用子类
                        putInPlanMonth.setCity(cityKey);// 城市
                        putInPlanMonth.setPlanYear(String.valueOf(year));// 年

                        String type = this.getValue(row.getCell(1)).trim();// 资源类型
                        if (!"".equals(type) && type.equals("固定资源")) {
                            putInPlanMonth.setResourceType("1");// 固定资源
                        }
                        else if (!"".equals(type) && type.equals("浮动资源")) {
                            putInPlanMonth.setResourceType("2");// 浮动资源
                        }
                        else if (!"".equals(type) && type.equals("购买资源")) {
                            putInPlanMonth.setPromotionType("1");// 购买资源
                        }
                        else if (!"".equals(type) && type.equals("赠送资源")) {
                            putInPlanMonth.setPromotionType("2");// 赠送资源
                        }
                        else {
                            uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，资源类型不能为空或格式错误！<br/>";
                            bool = false;
                        }

                        // 根据年度ID和资源类型查找
                        List<PutInPlanMonth> ppmList = resMonitorService.findPutMonthByNowTotalPutInAndType(putInPlanMonth);
                        if (ppmList != null && ppmList.size() != 0) {
                             uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，预算(月),该条记录重复！<br/>";
                            bool = false;
                            continue;
                        }

                        String putIn01 = this.getValue(row.getCell(2)).trim();// 1月
                        if (!"".equals(putIn01) && putIn01 != null) {
                            if (this.isFloathString(putIn01) && putIn01.length() <12) {
                                putInPlanMonth.setPutIn01(Double.parseDouble(putIn01));
                            }
                            else {
                                uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，1月投放量不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }
                        }
                        else {
                            putInPlanMonth.setPutIn01(0.0);// 为空时为0
                        }

                        String putIn02 = this.getValue(row.getCell(3)).trim();// 2月
                        if (!"".equals(putIn02) && putIn02 != null) {
                            if (this.isFloathString(putIn02) && putIn02.length() < 12) {
                                putInPlanMonth.setPutIn02(Double.parseDouble(putIn02));
                            }
                            else {
                                uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，2月投放量不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }
                        }
                        else {
                            putInPlanMonth.setPutIn02(0.0);// 为空时为0
                        }

                        String putIn03 = this.getValue(row.getCell(4)).trim();// 3月
                        if (!"".equals(putIn03) && putIn03 != null) {
                            if (this.isFloathString(putIn03) && putIn03.length() <12) {
                                putInPlanMonth.setPutIn03(Double.parseDouble(putIn03));
                            }
                            else {
                                uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，3月投放量不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }
                        }
                        else {
                            putInPlanMonth.setPutIn03(0.0);// 为空时为0
                        }

                        String putIn04 = this.getValue(row.getCell(5)).trim();// 4月
                        if (!"".equals(putIn04) && putIn04 != null) {
                            if (this.isFloathString(putIn04) && putIn04.length() <12) {
                                putInPlanMonth.setPutIn04(Double.parseDouble(putIn04));
                            }
                            else {
                                uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，4月投放量不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }
                        }
                        else {
                            putInPlanMonth.setPutIn04(0.0);// 为空时为0
                        }

                        String putIn05 = this.getValue(row.getCell(6)).trim();// 5月
                        if (!"".equals(putIn05) && putIn05 != null) {
                            if (this.isFloathString(putIn05) && putIn05.length() <12) {
                                putInPlanMonth.setPutIn05(Double.parseDouble(putIn05));
                            }
                            else {
                                uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，5月投放量不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }
                        }
                        else {
                            putInPlanMonth.setPutIn05(0.0);// 为空时为0
                        }

                        String putIn06 = this.getValue(row.getCell(7)).trim();// 6月
                        if (!"".equals(putIn06) && putIn06 != null) {
                            if (this.isFloathString(putIn06) && putIn06.length() <12) {
                                putInPlanMonth.setPutIn06(Double.parseDouble(putIn06));
                            }
                            else {
                                uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，6月投放量不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }
                        }
                        else {
                            putInPlanMonth.setPutIn06(0.0);// 为空时为0
                        }

                        String putIn07 = this.getValue(row.getCell(8)).trim();// 7月
                        if (!"".equals(putIn07) && putIn07 != null) {
                            if (this.isFloathString(putIn07) && putIn07.length() <12) {
                                putInPlanMonth.setPutIn07(Double.parseDouble(putIn07));
                            }
                            else {
                                uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，7月投放量不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }
                        }
                        else {
                            putInPlanMonth.setPutIn07(0.0);// 为空时为0
                        }

                        String putIn08 = this.getValue(row.getCell(9)).trim();// 8月
                        if (!"".equals(putIn08) && putIn08 != null) {
                            if (this.isFloathString(putIn08) && putIn08.length() <12) {
                                putInPlanMonth.setPutIn08(Double.parseDouble(putIn08));
                            }
                            else {
                                uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，8月投放量不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }
                        }
                        else {
                            putInPlanMonth.setPutIn08(0.0);// 为空时为0
                        }

                        String putIn09 = this.getValue(row.getCell(10)).trim();// 9月
                        if (!"".equals(putIn09) && putIn09 != null) {
                            if (this.isFloathString(putIn09) && putIn09.length() <12) {
                                putInPlanMonth.setPutIn09(Double.parseDouble(putIn09));
                            }
                            else {
                                uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，9月投放量不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }
                        }
                        else {
                            putInPlanMonth.setPutIn09(0.0);// 为空时为0
                        }

                        String putIn10 = this.getValue(row.getCell(11)).trim();// 10月
                        if (!"".equals(putIn10) && putIn10 != null) {
                            if (this.isFloathString(putIn10) && putIn10.length() <12) {
                                putInPlanMonth.setPutIn10(Double.parseDouble(putIn10));
                            }
                            else {
                                uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，10月投放量不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }
                        }
                        else {
                            putInPlanMonth.setPutIn10(0.0);// 为空时为0
                        }

                        String putIn11 = this.getValue(row.getCell(12)).trim();// 11月
                        if (!"".equals(putIn11) && putIn11 != null) {
                            if (this.isFloathString(putIn11) && putIn11.length() <12) {
                                putInPlanMonth.setPutIn11(Double.parseDouble(putIn11));
                            }
                            else {
                                uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，11月投放量不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }
                        }
                        else {
                            putInPlanMonth.setPutIn11(0.0);// 为空时为0
                        }

                        String putIn12 = this.getValue(row.getCell(13)).trim();// 12月
                        if (!"".equals(putIn12) && putIn12 != null) {
                            if (this.isFloathString(putIn12) && putIn12.length() <12) {
                                putInPlanMonth.setPutIn12(Double.parseDouble(putIn12));
                            }
                            else {
                                uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，12月投放量不是数字或长度不能大于12位！<br/>";
                                bool = false;
                            }
                        }
                        else {
                            putInPlanMonth.setPutIn12(0.0);// 为空时为0
                        }

                        if (bool) {
                            double sum = putInPlanMonth.getPutIn01() + putInPlanMonth.getPutIn02() + putInPlanMonth.getPutIn03()
                                    + putInPlanMonth.getPutIn04() + putInPlanMonth.getPutIn05() + putInPlanMonth.getPutIn06()
                                    + putInPlanMonth.getPutIn07() + putInPlanMonth.getPutIn08() + putInPlanMonth.getPutIn09()
                                    + putInPlanMonth.getPutIn10() + putInPlanMonth.getPutIn11() + putInPlanMonth.getPutIn12();
                            if (sourcePlanYear_Bool != null) {
                                if (!"".equals(type) && type.equals("固定资源")) {
                                    // System.out.println(sourcePlanYear_Bool.getId());
                                    // System.out.println(sourcePlanYear_Bool.getFixSourceAmount()+"____"+sum);
                                    if (sum != sourcePlanYear_Bool.getFixSourceAmount()) {
                                        uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，年度固定资源投放量不等于月度固定资源量总和！<br/>";
                                        continue;
                                    }
                                }
                                else if (!"".equals(type) && type.equals("浮动资源")) {
                                    // System.out.println(sourcePlanYear_Bool.getFloatSourceAmount()+"____"+sum);
                                    if (sum != sourcePlanYear_Bool.getFloatSourceAmount()) {
                                        uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，年度浮动资源投放量不等于月度浮动资源总和！<br/>";
                                        continue;
                                    }
                                }
                                else if (!"".equals(type) && type.equals("购买资源")) {
                                    // System.out.println(sourcePlanYear_Bool.getUnPromotionAmount()+"____"+sum);
                                    if (sum != sourcePlanYear_Bool.getUnPromotionAmount()) {
                                        uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，年度购买资源投放量不等于月度购买资源总和！<br/>";
                                        continue;
                                    }
                                }
                                else if (!"".equals(type) && type.equals("赠送资源")) {
                                    double num = sourcePlanYear_Bool.getPromotionAmount() + sourcePlanYear_Bool.getIncrePromotionAmount()
                                            + sourcePlanYear_Bool.getReturnSourceAmount();
                                    // System.out.println(num+"____"+sum);
                                    if (num != sum) {
                                        uploadErrorMessage_01 += sheetName + ",第" + (r + 1) + "行，年度赠送资源投放量不等于月度赠送资源总和！<br/>";
                                        continue;
                                    }
                                }
                            }

                            putInPlanMonth.setNowTotalPutIn(sum);

                            resMonitorService.savePutInPlanMonth(putInPlanMonth);// 添加预算(月)
                            this.deleteFile(fliePath);// 删除文件
                        }
                    }
                }
            }
            if ("".equals(uploadErrorMessage_01)) {
                noError_01 = "noError_01";
            }

        }
        catch (Exception e) {
            e.printStackTrace();
            this.deleteFile(fliePath);// 删除文件
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 判断是否日期
     * 
     * @param s
     * @return
     */
    private boolean isValidDate(String s) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        try {
            dateFormat.parse(s);
            return true;
        }
        catch (Exception e) {
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            return false;
        }
    }

    /**
     * 判断是否整数
     * 
     * @param testString
     * @return
     */
    private boolean isNumberString(String testString) {
        String numAllString = "0123456789";
        if (testString.length() <= 0)
            return false;
        for (int i = 0; i < testString.length(); i++) {
            String charInString = testString.substring(i, i + 1);
            if (!numAllString.contains(charInString))
                return false;
        }
        return true;
    }

    /**
     * 判断是否Float
     * 
     * @param testString
     * @return
     */
    public boolean isFloathString(String testString) {
        if (!testString.contains(".")) {
            return isNumberString(testString);
        }
        else {
            String[] floatStringPartArray = testString.split("\\.");
            if (floatStringPartArray.length == 2) {
                if (isNumberString(floatStringPartArray[0]) == true && isNumberString(floatStringPartArray[1]) == true)
                    return true;
                else
                    return false;
            }
            else
                return false;

        }
    }

    /**
     * 得到Excel表中的值
     * 
     * @param hssfCell
     *            Excel中的每一个格子
     * @return Excel中每一个格子中的值
     */
    public String getValue(Cell hssfCell) {
        if (hssfCell == null) {
            return "";
        }
        if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
            // 返回布尔类型的值
            return String.valueOf(hssfCell.getBooleanCellValue()).trim();
        }
        if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            if (HSSFDateUtil.isCellDateFormatted(hssfCell)) {
                SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
                Date dt = HSSFDateUtil.getJavaDate(hssfCell.getNumericCellValue());
                return dateformat.format(dt).trim();
            }
            else {
                // 返回数值类型的值
                String str = String.valueOf(hssfCell.getNumericCellValue()).trim();
                if (isFloathString(str)) {
                    double num = Double.parseDouble("0" + str.substring(str.indexOf(".")));
                    if (num == 0.0) {
                        int num2 = Integer.parseInt(str.substring(0, str.indexOf(".")));
                        return String.valueOf(num2);
                    }
                    else {
                        return str;
                    }
                }
            }
        }
        // 返回字符串类型的值
        return String.valueOf(hssfCell.getStringCellValue()).trim();
    }

    /**
     * 删除单个文件
     * 
     * @param sPath
     *            被删除文件的文件名
     * @return 单个文件删除成功返回true，否则返回false
     */
    public boolean deleteFile(String sPath) {
        boolean flag = false;
        File file = new File(sPath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }

    private Workbook create(InputStream inp) throws IOException, InvalidFormatException {
        if (!inp.markSupported()) {
            inp = new PushbackInputStream(inp, 8);
        }
        if (POIFSFileSystem.hasPOIFSHeader(inp)) {
            return new HSSFWorkbook(inp);
        }
        if (POIXMLDocument.hasOOXMLHeader(inp)) {
            return new XSSFWorkbook(OPCPackage.open(inp));
        }
        throw new IllegalArgumentException("你的excel版本目前poi解析不了");
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getCityKey() {
        return cityKey;
    }

    public void setCityKey(String cityKey) {
        this.cityKey = cityKey;
    }

    public String getCityValue() {
        return cityValue;
    }

    public void setCityValue(String cityValue) {
        this.cityValue = cityValue;
    }

    public List<Mediasumm> getMediaSummaries() {
        return mediaSummaries;
    }

    public void setMediaSummaries(List<Mediasumm> mediaSummaries) {
        this.mediaSummaries = mediaSummaries;
    }

    public void setResMonitorService(ResMonitorService resMonitorService) {
        this.resMonitorService = resMonitorService;
    }

    public String getUploadErrorMessage() {
        return uploadErrorMessage;
    }

    public void setUploadErrorMessage(String uploadErrorMessage) {
        this.uploadErrorMessage = uploadErrorMessage;
    }

    public String getNoError() {
        return noError;
    }

    public void setNoError(String noError) {
        this.noError = noError;
    }

    public File getUploadFile_01() {
        return uploadFile_01;
    }

    public void setUploadFile_01(File uploadFile_01) {
        this.uploadFile_01 = uploadFile_01;
    }

    public String getUploadFile_01FileName() {
        return uploadFile_01FileName;
    }

    public void setUploadFile_01FileName(String uploadFile_01FileName) {
        this.uploadFile_01FileName = uploadFile_01FileName;
    }

    public String getUploadFile_01ContentType() {
        return uploadFile_01ContentType;
    }

    public void setUploadFile_01ContentType(String uploadFile_01ContentType) {
        this.uploadFile_01ContentType = uploadFile_01ContentType;
    }

    public void setMediaPriceService(MediaPriceService mediaPriceService) {
        this.mediaPriceService = mediaPriceService;
    }

    public String getCostType() {
        return costType;
    }

    public void setCostType(String costType) {
        this.costType = costType;
    }

    public String getCostSubtype() {
        return costSubtype;
    }

    public void setCostSubtype(String costSubtype) {
        this.costSubtype = costSubtype;
    }

    public File getUploadFile_02() {
        return uploadFile_02;
    }

    public void setUploadFile_02(File uploadFile_02) {
        this.uploadFile_02 = uploadFile_02;
    }

    public String getUploadFile_02FileName() {
        return uploadFile_02FileName;
    }

    public void setUploadFile_02FileName(String uploadFile_02FileName) {
        this.uploadFile_02FileName = uploadFile_02FileName;
    }

    public String getUploadFile_02ContentType() {
        return uploadFile_02ContentType;
    }

    public void setUploadFile_02ContentType(String uploadFile_02ContentType) {
        this.uploadFile_02ContentType = uploadFile_02ContentType;
    }

    public String getUploadErrorMessage_01() {
        return uploadErrorMessage_01;
    }

    public void setUploadErrorMessage_01(String uploadErrorMessage_01) {
        this.uploadErrorMessage_01 = uploadErrorMessage_01;
    }

    public String getNoError_01() {
        return noError_01;
    }

    public void setNoError_01(String noError_01) {
        this.noError_01 = noError_01;
    }

}
